#include "stdafx.h"
#include "CommandList.h"

CommandList* CommandList::instance = 0;

CommandList::CommandList() : sync(false) {
	packer = new DataPacker();
}

void CommandList::setNetworkController(NetworkController* nc) {
	network = nc;
}

NetworkController* CommandList::getNetworkController() {
	return network;
}

CommandList::~CommandList () {
	delete instance;
}

list<Command*>* CommandList::getList() {
	return &commands;
}

void CommandList::addList(list<Command*> &list) {
	std::list<Command*>::iterator it;
	for ( it=list.begin() ; it != list.end() ; it++ ) {
		commands.push_back((*it));
	}
}

void CommandList::execute(Model* model) {
	std::list<Command*>::iterator it;
	for ( it=commands.begin() ; it != commands.end() ; it++ ) {
		(*it)->execute(model);
	}
	packer->addCommands(commands);
	network->sendCommands(commands);	
	
	executeNetwork(model);
	
	commands.clear();
}

void CommandList::executeNetwork(Model* model) {
	std::list<Command*>::iterator it;
	for ( it=networkList.begin() ; it != networkList.end() ; it++ ) {
		(*it)->execute(model);
	}
	packer->addCommands(networkList);
	networkList.clear();
}

void CommandList::add(Command* obj) {
	commands.push_back(obj);
}

void CommandList::addNetwork(Command* obj) {
	networkList.push_back(obj);
}
	
CommandList* CommandList::getInstance() {
	if (!instance) {
		instance = new CommandList();
	}
	return instance;
}

GameState* CommandList::getGameState() {
	return state;
}

void CommandList::setGameState(GameState* gs) {
	state = gs;
}
